<?php
class Admin_SupportController extends Apps_Controller_Action_Admin {

	public function indexAction() {
		// redirect to list support
		$this->_redirect('/admin/support/list');
	}

	public function listAction() {
		$this->actResolve('act_delete',
						  'delete', null, null, array('id' => $this->_getParam('item_checker')),
					 	  true);

		$tbSupport = new M_DbTable_Support();
		$grid 	= new Apps_Plugin_Grid('supportGrid');

		$grid->setData($tbSupport->getList())
			 ->setTableCaption('List Suppoter')
			 ->setTableColumnFormat(array(
			 		array('title'		=> 'ID',
			 			  'dataField'	=> 'id',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Suppoter',
			 			  'dataField'	=> 'supporter_name'),
			 		array('title'		=> 'Type',
			 			  'class'		=> 'center',
	                      'html'        => function($rs) {
	                                            $labelLabourStatus = new Apps_View_Helper_LabelSupportType();
	                                            return $labelLabourStatus->labelSupportType($rs['type']);
	                                        }),
			 		array('title'		=> 'Account',
			 			  'dataField'	=> 'account',
			 			  'class'		=> 'center'),
			 		array('title'       => 'Edit',
			 			  'html'        => array('<a href="/admin/support/edit?id=%s"><i class="icon-edit" title="Sửa"></i></a>',
			 			  				   array('id')),
			 			  'width'       => '2%'),
			 		array('title'      => 'Delete',
			 			  'html'       => array('<a href="/admin/support/delete?id=%s"><i class="icon-remove must-confirm " title="Xóa"></i></a>',
			 			  				  array('id')),
			 			  'width'      => '2%')
			 		));
		$this->view->grid = $grid;
	}

	public function addAction() {
		$rq = $this->_request;

		$tbSupport = new M_DbTable_Support();
		$f 	= new Admin_Form_AddSupport();

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbSupport->insert($f->getValues())
					? self::$msgAddSuccess
					: self::$msgAddFailed);

				$this->_helper->redirector('index', null, null);
			}
		}
		$this->view->form = $f;
	}

	public function editAction() {
		$rq = $this->_request;

		$supportId = intval($rq->getParam('id'));
		$tbSupport = new M_DbTable_Support();
		$f = new Admin_Form_AddSupport();

		if(!$supportId || !($r = $tbSupport->fetchRow(array('id = ?' => $supportId))->toArray())) {
			$this->_flashMessenger->addMessage(self::$msgNoRecordFound);
			$this->goToActionIndex();
		}

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbSupport->safelyUpdate($f->getValues(), array('id = ?' => $supportId))
					? self::$msgAddSuccess
					: self::$msgAddFailed);

				$this->_helper->redirector('index', null, null);
			}
		} else {
			$f->populate($r);
		}
		$f->setLegend('Edit Suppoter');
		$this->view->form = $f;
		$this->render('add');
	}

	public function deleteAction() {
		$this->disableLayout(true, true);

		$rq = $this->_request;
		$supportIds = $rq->getParam('id');
		$tbSupport = new M_DbTable_Support();

		if(is_numeric($supportIds)) {
			$this->_flashMessenger->addMessage(
						$tbSupport->safelyDelete(array('id = ?' => $supportIds)) ?
						self::$msgDeleteSuccess :
						self::$msgDeleteError
					);
			$this->goToActionIndex();
		} elseif (is_array($supportIds)) {
			foreach ($supportIds as $faqId) {
				$tbSupport->safelyDelete(array('id = ?' => $faqId));
			}
			$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
			$this->goToActionIndex();
		} else {
			$this->_flashMessenger->addMessage(self::$msgNoRecordFound);
			$this->goToActionIndex();
		}
	}


}